import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import axios from 'axios'
export default new Vuex.Store({
  state: {
    address: []
  },
  mutations: {
    SET_ADDRESS(state, payload) {
      state.address = payload
    },
    MODIFY_ADDRESS(state, payload) {
      const o = state.address.find(item => item.id === payload.id)

      if(o) {
        Object.assign(o, payload)
      }
    },
    DELETE_ADDRESS(state, id) {
      state.address = state.address.filter(item => item.id !== id)
    },
    ADD_ADDRESS(state, payload) {
      state.address.push(payload)
    }
  },
  actions: {
    async GET_ADDRESS({ commit }) {
      const resp = await axios.get('/api/address')
      commit('SET_ADDRESS', resp.data.data)
    }
  },
  modules: {
  }
})
